package com.elong.android.specialhouse.activity;


import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.elong.android.specialhouse.JSONConstants;
import com.elong.android.specialhouse.R;
import com.elong.android.specialhouse.entity.HouseFacility;
import com.elong.android.specialhouse.entity.response.OptionConfig;
import com.elong.android.specialhouse.utils.GetFacilitiesIcon;
import com.elong.android.specialhouse.utils.StringUtils;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class PopupWindowUtils {

	/**
	 * 
	 * @Title: popupFacilities
	 * @Description: 弹出设施列表框
	 * @param context
	 * @param communalFacilities 公共设施
	 * @param roomFacilities 房间设施
	 * @param specialFacilities 其他设施

	 */
	public final static void popupFacilities(final Activity context, List<HouseFacility> communalFacilities,
											 List<HouseFacility> roomFacilities,List<HouseFacility> specialFacilities){
		ArrayList<HouseFacility> mFacilitiesData=new ArrayList<HouseFacility>();
		if(roomFacilities!=null && !roomFacilities.isEmpty()){
			HouseFacility room = new HouseFacility();
			room.FacilityNameCN = context.getResources().getString(R.string.facility_room);
			mFacilitiesData.add(room);
			for(HouseFacility facilities:roomFacilities){
				mFacilitiesData.add(facilities);
			}
		}

		if(communalFacilities!=null && !communalFacilities.isEmpty()){
			HouseFacility common = new HouseFacility();
			common.FacilityNameCN = context.getResources().getString(R.string.facility_public);
			mFacilitiesData.add(common);
			for(HouseFacility facilities:communalFacilities){
				mFacilitiesData.add(facilities);
			}
		}
		
		if(specialFacilities!=null && !specialFacilities.isEmpty()){
			HouseFacility special = new HouseFacility();
			special.FacilityNameCN = context.getResources().getString(R.string.facility_other);
			mFacilitiesData.add(special);
			for(HouseFacility facilities:specialFacilities){
				mFacilitiesData.add(facilities);
			}
		}

		final View v = LayoutInflater.from(context).inflate(R.layout.popwindow_apartment_facilities_layout, null);
		final PopupWindow window = new PopupWindow(v, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, true);
		v.setOnTouchListener(new View.OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if (window != null && window.isShowing()) {
					window.dismiss();
				}
				return false;
			}
		});

		window.setAnimationStyle(R.style.popoutwindow_animation);
		window.setBackgroundDrawable(new ColorDrawable(0x0000000));
		//初始化列表数据
		ListView listView=(ListView)v.findViewById(R.id.facilities_list);
		FacilitiesListAdapter adapter=new FacilitiesListAdapter(context, mFacilitiesData);
		listView.setAdapter(adapter);

		window.showAtLocation(context.getWindow().getDecorView(), Gravity.CENTER, 0, 0);
	}

public static  class FacilitiesListAdapter extends BaseAdapter{
		private Context context;
		private ArrayList<HouseFacility> mFacilitiesData;
		private List<OptionConfig> facilityList;
		public FacilitiesListAdapter(Context context,ArrayList<HouseFacility> mFacilitiesData){
			this.context=context;
			this.mFacilitiesData=mFacilitiesData;
			this.facilityList = initFacility(context);
		}

		@Override
		public int getCount() {
			return mFacilitiesData.size();
		}

		@Override
		public Object getItem(int position) {
			return null;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			HouseFacility facility = mFacilitiesData.get(position);
			String facilityName = facility.FacilityNameCN;
			int facilityId = facility.FacilityID;
			//服务器返回设施id，设施名称为空
			if (StringUtils.isEmpty(facilityName)) {
				convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_facilities_list_content, null);
				OptionConfig item = findInFacilityList(facilityId);
				if (item != null) {
					convertView.setVisibility(View.VISIBLE);
					TextView tv = (TextView) convertView.findViewById(R.id.item_facilities_text);
					tv.setText(item.getOptionName());

					ImageView iv = (ImageView) convertView.findViewById(R.id.item_facilities_image);
					Bitmap icon = GetFacilitiesIcon.getInstance(context.getApplicationContext()).getImage(item.getOptionValue());
					if (icon != null) {
						iv.setImageBitmap(icon);
					} else {
						iv.setVisibility(View.INVISIBLE);
					}
				}
				else{
					convertView.setVisibility(View.GONE);
				}
			} else if (facilityName.equals(context.getResources().getString(R.string.facility_room))
					|| facilityName.equals(context.getResources().getString(R.string.facility_public))
					|| facilityName.equals(context.getResources().getString(R.string.facility_other))) {
				convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_facilities_list_head, null);
				TextView tv = (TextView) convertView.findViewById(R.id.item_facilities_header);
				tv.setText(facilityName);
			}
			return convertView;
		}
		public OptionConfig findInFacilityList(int facilityId){
			for(OptionConfig item : facilityList){
				if(facilityId == item.getOptionValue()){
					return item;
				}
			}
			return null;
		}
	}
	public static List<OptionConfig> initFacility(Context context) {
		List<OptionConfig> facilities = new ArrayList<OptionConfig>();
		try{
			//得到资源中的Raw数据流
			InputStream in = context.getResources().openRawResource(R.raw.facilitieslist);
			//得到数据的大小
			int length = in.available();
			byte [] buffer = new byte[length];
			//读取数据
			in.read(buffer);
			JSONObject result = JSONObject.parseObject(new String(buffer));
			JSONArray arrayResult = result.getJSONArray(JSONConstants.ATTR_OPTIONCONFIG_LIST);
			if (null == arrayResult ) {
				return facilities;
			}else {
				for(int i=0;i<arrayResult.size();i++){
					OptionConfig parseResult= JSON.parseObject(arrayResult.get(i).toString(), OptionConfig.class);
					facilities.add(parseResult);
				}
			}
			//关闭
			in.close();
		}catch(Exception e){
			e.printStackTrace();
		}
		return facilities;
	}


}
